/**
 * 
 */
package com.res.common.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.res.simulator.handler.CommandExecutorHandler;
import com.res.simulator.model.CommandExecutorModel;
import com.res.simulator.view.CommandExecutorView;

/**
 * @author Dhanushka Ariyasingha.
 *
 */
@Controller
@SessionAttributes
public class CommandExecutorController extends BaseController {

	@RequestMapping("/commandexecutorhome")
	public String home(@ModelAttribute("CommandExecutorModel") CommandExecutorModel commandExecutorModel) {
		
		
		
		return "commandexecutorlayout";
	}
	
	@RequestMapping(value = "/executeCommand", method = RequestMethod.GET, params = "Execute")
	protected String executeCommand(@ModelAttribute("CommandExecutorModel") CommandExecutorModel commandExecutorModel) throws Exception {
		
		final CommandExecutorHandler handler = new CommandExecutorHandler();
		final CommandExecutorView view = handler.process(commandExecutorModel);

		List<CommandExecutorView> viewCollection = (List<CommandExecutorView>) getSession().getAttribute("CommandExecutorViewCollection");

		if (null == viewCollection) {
			viewCollection = new ArrayList<CommandExecutorView>();
		}

		viewCollection.add(view);

		getSession().setAttribute("CommandExecutorViewCollection", viewCollection);

		return "redirect:commandexecutorhome.htm";
	}
		
	@RequestMapping(value = "/executeCommand", method = RequestMethod.GET, params = "Clear")
	protected String clear(@ModelAttribute("CommandExecutorModel") CommandExecutorModel commandExecutorModel) throws Exception {

		final List<CommandExecutorView> viewCollection = (List<CommandExecutorView>) getSession().getAttribute("CommandExecutorViewCollection");

		if (null != viewCollection) {
			viewCollection.clear();
		}

		getSession().setAttribute("CommandExecutorView", viewCollection);

		return "redirect:commandexecutorhome.htm";
	}
		
}
